package com.kaka.bus.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.kaka.bus.AppCenter;
import com.kaka.bus.BaiQiShi;
import com.kaka.bus.bean.UserInfo;
import com.kaka.bus.listener.NetListener;
import com.kaka.bus.util.ScreenUtils;
import com.kaka.bus.util.StatusBarUtil;
import com.kaka.bus.util.UserUtil;
import com.kaka.bus.util.logger.Logger;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import cn.jpush.android.api.JPluginPlatformInterface;
import me.yokeyword.fragmentation.SupportActivity;


/**
 * Created by zhangqi on 2017/12/20.
 * e-mail : ${email}
 * desc :
 */

public class ConfigActivity extends SupportActivity implements NetListener{

    private JPluginPlatformInterface pHuaweiPushInterface;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 屏幕适配
         */
        if (ScreenUtils.isPortrait()) {
            ScreenUtils.adaptScreen4VerticalSlide(this, 360);
        } else {
            ScreenUtils.adaptScreen4HorizontalSlide(this, 360);
        }

        Logger.i("进入"+this.getClass().getName());
        AppCenter.addActivity(this);

        pHuaweiPushInterface = new JPluginPlatformInterface(this.getApplicationContext());

        if(needFullScreen()){
            StatusBarUtil.setTranslucentForImageViewInFragment(this,null);
        }
    }

    protected boolean needFullScreen(){
        return false;
    }

    @Override
    protected void onStart() {
        super.onStart();
        pHuaweiPushInterface.onStart(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        pHuaweiPushInterface.onStart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        /**
         */
        BaiQiShi.initBaiQiShi(this.getApplicationContext());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == JPluginPlatformInterface.JPLUGIN_REQUEST_CODE) {
            pHuaweiPushInterface.onActivityResult(this, requestCode, resultCode, data);
        }
    }


    public void setUserInfo(UserInfo person){
        UserUtil.setCache(UserUtil.USERINFO,person);
    }

    public UserInfo getUserInfo(){
        return UserUtil.getCache(UserUtil.USERINFO);
    }

    public String getUserId(){
        UserInfo userInfo = getUserInfo();
        if(userInfo == null){
            return null;
        }else{
            return getUserInfo().getId();
        }
    }

    public String getToken(){
        UserInfo userInfo = getUserInfo();
        if(userInfo == null){
            return null;
        }else{
            return getUserInfo().getTokenId();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Logger.i("退出"+this.getClass().getName());
        AppCenter.removeActivity(this);
    }

    protected boolean isMainThread(){
        return Looper.getMainLooper() == Looper.myLooper();
    }

    @Override
    public void onNetChange(boolean isConnect) {
        //网络状态改变

        List<Fragment> fragments = getSupportFragmentManager().getFragments();

        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            if(fragment instanceof NetListener){
                NetListener net = (NetListener) fragment;
                net.onNetChange(isConnect);
            }
        }
    }
}
